package com.draven.controller;

import com.draven.annotation.Log;
import com.draven.common.Result;
import com.draven.entity.FileCategory;
import com.draven.service.FileCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 文件分类控制器
 */
@RestController
@RequestMapping("/fileCategory")
@Validated
public class FileCategoryController {

    @Autowired
    private FileCategoryService fileCategoryService;

    /**
     * 获取用户的文件分类列表
     */
    @GetMapping("/list")
    public Result<List<FileCategory>> getCategoryList(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        List<FileCategory> categoryList = fileCategoryService.getCategoriesByUserId(userId);
        return Result.success(categoryList);
    }

    /**
     * 添加文件分类
     */
    @PostMapping("/add")
    @Log(module = "文件分类", operationType = "ADD", description = "添加文件分类")
    public Result<FileCategory> addCategory(
            HttpServletRequest request,
            @NotBlank(message = "分类名称不能为空") @RequestParam String name,
            @RequestParam(required = false) Integer sort,
            @RequestParam(required = false) String icon) {
        
        Long userId = (Long) request.getAttribute("userId");
        
        FileCategory fileCategory = new FileCategory();
        fileCategory.setUserId(userId);
        fileCategory.setName(name);
        
        if (sort != null) {
            fileCategory.setSort(sort);
        }
        
        if (icon != null) {
            fileCategory.setIcon(icon);
        }
        
        FileCategory result = fileCategoryService.addCategory(fileCategory);
        return Result.success(result);
    }

    /**
     * 更新文件分类
     */
    @PutMapping("/update")
    @Log(module = "文件分类", operationType = "UPDATE", description = "更新文件分类")
    public Result<Void> updateCategory(
            HttpServletRequest request,
            @NotNull(message = "分类ID不能为空") @RequestParam Long id,
            @NotBlank(message = "分类名称不能为空") @RequestParam String name,
            @RequestParam(required = false) Integer sort,
            @RequestParam(required = false) String icon) {
        
        Long userId = (Long) request.getAttribute("userId");
        
        FileCategory fileCategory = new FileCategory();
        fileCategory.setId(id);
        fileCategory.setUserId(userId);
        fileCategory.setName(name);
        
        if (sort != null) {
            fileCategory.setSort(sort);
        }
        
        if (icon != null) {
            fileCategory.setIcon(icon);
        }
        
        boolean result = fileCategoryService.updateCategory(fileCategory);
        return result ? Result.success() : Result.error("更新分类失败");
    }

    /**
     * 删除文件分类
     */
    @DeleteMapping("/delete")
    @Log(module = "文件分类", operationType = "DELETE", description = "删除文件分类")
    public Result<Void> deleteCategory(
            HttpServletRequest request,
            @NotNull(message = "分类ID不能为空") @RequestParam Long id) {
        
        Long userId = (Long) request.getAttribute("userId");
        boolean result = fileCategoryService.deleteCategory(id, userId);
        return result ? Result.success() : Result.error("删除分类失败");
    }

    /**
     * 获取文件分类详情
     */
    @GetMapping("/detail")
    public Result<FileCategory> getCategoryDetail(
            @NotNull(message = "分类ID不能为空") @RequestParam Long id) {
        
        FileCategory category = fileCategoryService.getCategoryById(id);
        if (category == null) {
            return Result.error(404, "分类不存在");
        }
        return Result.success(category);
    }
} 